package com.hdax.dm.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.common.utils.CommonResponse;
import com.hdax.dm.common.utils.ResponseUtil;
import com.hdax.dm.dto.OrderListDto;
import com.hdax.dm.entity.order.DmOrder;
import com.hdax.dm.mappers.DmOrderMapper;
import com.hdax.dm.mapstructs.OrderListDtoMapper;
import com.hdax.dm.service.OrderService;
import com.hdax.dm.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<DmOrderMapper, DmOrder> implements OrderService {

    @Autowired
    private OrderListDtoMapper orderListDtoMapper;
    @Override
    public CommonResponse<List<OrderListDto>> queryorderlist(String orderNo, String orderType, String orderTime, String token) {
        //查询当前登录用户的订单
        Long userId = null;
        if(StrUtil.isNotEmpty(token)){
            userId = Long.valueOf(TokenUtil.parseToken(token).get("userId"));
        }
        QueryWrapper<DmOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        queryWrapper.orderByDesc("createdTime");
        List<DmOrder> orders = list(queryWrapper);
        return ResponseUtil.returnSuccess(orderListDtoMapper.toTargetList(orders));
    }

    @Override
    public DmOrder getOrder(String orderNo) {
        QueryWrapper<DmOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderNo",orderNo);
        return getOne(queryWrapper);
    }
}
